import { getPhoneNumber, handleLogin } from "../../api/user";

import { setToken, clearToken } from "../../utils/storage";
import { toDefaultPage } from '../../utils/navigate'
// let loading = false;
let openId = "";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    wxPhone: "",
    phone: "",
    pwd: "",
    openId: "",
    back: false,
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      back: getCurrentPages().length > 1,
      openId: getApp().globalData.openId,
    });
  },

  async getPhoneByWx(e) {
    let { openId } = getApp().globalData;
    let { iv, encryptedData } = e.detail;
    try {
      let result = await getPhoneNumber({ openId, iv, encryptedData });
 

      if (result.phone) {
        this.setData({ wxPhone: result.phone });
        this.setData({ phone: result.phone });
      } else {
        wx.showToast({ title: "获取手机号失败！", icon: "none" });
      }
    } catch (error) {
      wx.showToast({ title: "获取手机号失败！", icon: "none" });
    }
  },

  phoneInput(e) {
    this.setData({ phone: e.detail.value });
  },

  pwdInput(e) {
    this.setData({ pwd: e.detail.value });
  },

  toLogin() {
    wx.navigateTo({ url: "/pages/login/login" });
  },

  bindRegister() {
    let { openId } = getApp().globalData;
    wx.showToast({
      title: "登录成功",
      icon: "none",
    });
 
    // if (!/^[1][0-9][0-9]{9}$/.test(this.data.phone)) {
    //   wx.showToast({
    //     title: "请输入合法的手机号",
    //     icon: "none",
    //   });
    //   return;
    // }
    if (this.data.phone.length !== 11) {
      wx.showToast({
        title: "请输入合法的手机号",
        icon: "none",
      });
      return;
    }
    if (!this.data.phone) {
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
      });
      return;
    }
    if (!this.data.pwd) {
      wx.showToast({
        title: "请输入密码",
        icon: "none",
      });
      return;
    }
    clearToken("");
    handleLogin({
      phonenumber: this.data.phone,
      passwd: this.data.pwd,
      wx_openid_xin: openId || "",
    })
      .then((res) => {
         

        if (res.token) {
          wx.showToast({ title: "登录成功", icon: "none" });
          setToken(res.token);
          getApp().globalData.wx_openid = res.wx_openid;
          wx.setStorageSync("openId", res.wx_openid);
          toDefaultPage()
        } else {
          wx.showToast({ title: res.errMsg, icon: "none" });
        }
      })
      .catch((err) => {
        wx.showToast({ title: '手机号或密码错误！', icon: "none" });
      });
  },
  toRegister() {
    // wx.showToast({
    //   title: '去注册'
    // })
    wx.redirectTo({
      url: "/pages/regist/regist",
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
